package cn.edu.nwpu.warehousemanagement.pojo;

import cn.edu.nwpu.warehousemanagement.constantEnum.OperationType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;


@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "inventory_record") // 显式指定表名
public class InventoryRecord extends BaseEntity {

    // 操作类型：入库/出库（对应表字段type）
    @Enumerated(EnumType.STRING)
    @Column(name = "type", length = 20, nullable = false, columnDefinition = "VARCHAR(20)")
    private OperationType type;

    // 操作员ID（外键关联user表）
    @Column(name = "operator_id", nullable = false)
    private Long operatorId;

    // 关联订单ID（可为空）
    @Column(name = "related_order_id")
    private Long relatedOrderId;

    // 操作时间（默认当前时间）
    @Column(name = "operation_time", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private LocalDateTime operationTime;

    //批次号
    @Column(name = "batch_id", length = 20, nullable = false)
    private String batchId;


    // 关联多个货品（通过中间表inventory_freight）
    @OneToMany
    @JoinTable(
            name = "inventory_freight", // 中间表名
            // 显式指定当前实体（InventoryRecord）到中间表的外键约束名
            foreignKey = @ForeignKey(name = "fk_inventory_freight_record_id"),
            // 中间表中指向当前记录的外键
            joinColumns = @JoinColumn(name = "record_id", nullable = false),
            // 显式指定关联实体（Freight）到中间表的外键约束名
            inverseForeignKey = @ForeignKey(name = "fk_inventory_freight_freight_id"),
            // 中间表中指向货品的外键
            inverseJoinColumns = @JoinColumn(name = "freight_id", nullable = false, unique = false)
    )
    private List<Freight> freights;

    public InventoryRecord(OperationType operationType, Long loginUserId, LocalDateTime now, String batchId) {
        this.type = operationType;
        this.operatorId = loginUserId;
        this.operationTime = now;
        this.batchId = batchId;
    }
}